<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Cirpho extends Public_Controller
{
private $rules = array(
		array(
			'field' => 'request_name',
			'label' => 'lang:request_name_label',
			'rules' => 'required|trim|max_length[80]'
		),
		array(
			'field' => 'company_name',
			'label' => 'lang:request_company_name_label',
			'rules' => 'trim|max_length[80]'
		),
		array(
			'field' => 'request_email',
			'label' => 'lang:request_email_label',
			'rules' => 'required|trim|valid_email|max_length[80]'
		),
		array(
			'field' => 'request_phone',
			'label' => 'lang:request_phone_number_label',
			'rules' => 'trim'
		),
		array(
			'field' => 'project_detail',
			'label' => 'lang:request_project_detail_label',
			'rules' => 'trim'
		),
		array(
			'field' => 'service_require',
			'label' => 'lang:request_service_require_label',
			'rules' => 'required'
		)
	);

	public function __construct()
	{
		parent::__construct();
		$this->lang->load('request');
	}

	function index()
	{
		$this->load->library('form_validation');
		$this->load->helper('form');

		$this->form_validation->set_rules($this->rules);

		// If the user has provided valid information
		if ($this->form_validation->run())
		{
			// The try to send the email
			if ($this->_send_email())
			{
				$data = array();
				
				$input = $this->input->post();
				$input['sender_ip'] = $this->input->ip_address();
				$input['sender_os'] = $this->agent->platform();
				$service_require_str ="";
				foreach ($input['service_require'] as $service_require){
					$service_require_str .= $service_require.",";
				}
				$service_require_str = substr($service_require_str, 0, strlen($service_require_str) - 1);
				$input['service_require_str'] = $service_require_str;
				
				$this->load->model('request_quotes_m');
				
				$this->request_quotes_m->insert_request($input);
				$this->template->build('success', $data);
				return;
			}
			else
			{
				$message = $this->attribute('error', lang('contact_error_message'));

				$data['messages']['error'] = $message;
			}
		}

		// Set the values for the form inputs
		foreach ($this->rules as $rule)
		{
			$form_values->{$rule['field']} = set_value($rule['field']);
		}
		$this->load->model('request_quote_services_m');
		$data['services'] = $this->request_quote_services_m->get_service();
		$data['form_values']	= $form_values;
		$this->template->build('form', $data);
	}

	function _send_email($subjects = array())
	{
		$this->load->library('email');
		$input = $this->input->post();
		
		$this->load->model('settings_m');
		$emailServer = $this->settings_m->get("server_email");
		
		$this->email->from($emailServer->value, 'Funnyfox');
		
		$emailContace = $this->settings_m->get("contact_email");
		$this->email->to($emailContace->value);
		$this->email->subject(lang('request_email_subject'));
		$this->email->message('<html>
									<body>
										<h2>'.lang('request_email_header').'</h2>
										<table>
											<tr>
												<td><strong>'.lang('request_name_label').'</strong></td>
												<td>'.$input['request_name'].'</td>
											</tr>
											<tr>
												<td><strong>'.lang('request_company_name_label').'</strong></td>
												<td>'.$input['company_name'].'</td>
											</tr>
											<tr>
												<td><strong>'.lang('request_email_label').'</strong></td>
												<td>'.$input['request_email'].'</td>
											</tr>
											<tr>
												<td><strong>'.lang('request_phone_number_label').'</strong></td>
												<td>'.$input['request_phone'].'</td>
											</tr>
											<tr>
												<td><strong>'.lang('request_project_detail_label').'</strong></td>
												<td>'.$input['project_detail'].'</td>
											</tr>
										</table>
									</body>
								</html>');
		
//$lang['request_service_require_label']		= 'What Services do you require';

		$this->email->send();

		return TRUE;
	}
}